<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    // 初始化方法
    public function _initialize(){
        $cate = M('cate');
        // 查询导航
        $nav = $cate->where("is_nav = 1")->select();
//        dump($nav);
        $this->assign('nav',$nav);
		
		//百度主动推送(实时)
		$this->send_bd();
    }
	//百度主动推送(实时)
	public function send_bd(){
		$conf=D('conf');
		$news=D('news');
		$nowid=$conf->where("id=1")->getField('value');//dump($nowid);die;
		$maxid=$news->order('id desc')->limit(1)->getField('id');//dump($maxid); 
        if($nowid>0){
            $getarc=$news->where("id>".$nowid)->field('id')->order('id asc')->limit('160')->select();//dump($getarc);
            if(empty($getarc))$conf->where("id=1")->setField('value',1);//循环重新推送
            $urls = array();
            foreach($getarc as $i=>$one){
                $urls[]='http://zixun.xiangtws.com/Home/News/info/nid/'.$one['id'];
            }//end foreach
            //dump($urls);die;
            $api = 'http://data.zz.baidu.com/urls?site=zixun.xiangtws.com&token=ckHGjgB07EUCuENQ';
            $ch = curl_init();
            $options =  array(
                CURLOPT_URL => $api,
                CURLOPT_POST => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => implode("\n", $urls),
                CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
            );
            curl_setopt_array($ch, $options);
            $result = curl_exec($ch);
            $result =json_decode($result);
            if($result->success>0){
                //更新主动推送url的id节点
				//dump(count($getarc));
                $conf->where("id=1")->setField('value',$getarc[count($getarc)-1]['id']); 
            }//end if
            //dump( $result);
        }//end if

	}//end fun
}//end class